<?php
/**
 * Created by PhpStorm.
 * User: yuyiwen
 * Date: 2018/9/22
 * Time: 上午10:01
 */

namespace app\admin\controller;


use think\Controller;
use think\Request;

class Common extends Controller
{
    public function _initialize ()
    {
        $request = Request::instance();
        $module = $request->module();
        $controller = $request->controller();
        $action = $request->action();
        # 检测用户是否有登录
        $this->checkLogin();

        # 检测用户是否有权限访问
        $this->checkPermission();

        # 获取权限菜单
        $menus = $this->getMenus ();

        $this->assign('menus' , $menus);
        $this->assign('current_name' , strtolower($module . '/' . $controller . '/' . $action));
    }

    public function checkLogin ()
    {
        if (session('admin_id') == null || empty(session('admin_name')))
        {
            $this->error('请登录' , url('log/login'));
        }
    }
    # 检测权限
    public function checkPermission () {
        # 获取角色列表
        $roles = session('roles');
        if ($roles != 1) {
            $permission_list = db('RolePermission')->where(['role_id' => ['in', $roles]])->select();
            $permissions = [];
            foreach ($permission_list as $key => $value) {
                $permissions[] = $value['permission_id'];
            }
            $permissions = implode(array_unique($permissions), ',');


            $permission_list = db('permission')->where(['id' => ['in', $permissions]])->select();
            $hasPermission = false; // 0 代表没有权限
            foreach ($permission_list as $key => $value) {
//            $value['name']
                $request = Request::instance();
                $module = $request->module();
                $controller = $request->controller();
                $action = $request->action();

                if (strtolower($module . '/' . $controller . '/' . $action) == strtolower($value['name'])) {
                    $hasPermission = true;
                    break;
                }
            }
        }else {
            $hasPermission = true;
        }

        if (!$hasPermission) {
            $this->error('你没有权限访问' );
        }


    }

    // 获取权限菜单
    public function getMenus () {
        $roles = session('roles');

        $permission_list = db('RolePermission')->where(['role_id' => ['in', $roles]])->select();
        $permissions = [];
        foreach ($permission_list as $key => $value) {
            $permissions[] = $value['permission_id'];
        }
        $permissions = implode(array_unique($permissions), ',');

        $db = db('permission');
        if ($roles != 1) {
            $db = $db->where(['id' => ['in', $permissions]]);
        }
        $menus =$db->where(['type' => 0])->order('ords')->select();

        $tmp_menus = $menus;
//        header('content-type:text/html;charset=utf-8');
        foreach ( $tmp_menus as $key1 => $value1) {
            foreach ($tmp_menus as $key2 => $value2) {
                if ($value1['id'] == $value2['pid']) {
                    $menus[$key1]['son'][] = $value2;
                    unset($menus[$key2]);
                }
            }
        }

        return $menus;
    }
}